package com.zijie.hosptal.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.aop.AutoFill;
import com.zijie.hosptal.aop.OperationType;
import com.zijie.hosptal.exception.DsException;
import com.zijie.hosptal.mapper.DoctorMapper;
import com.zijie.hosptal.mapper.DoctorSchedulesMapper;
import com.zijie.hosptal.pojo.po.Doctor;
import com.zijie.hosptal.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {
    private DoctorMapper doctorMapper;
    private DoctorSchedulesMapper doctorSchedulesMapper;

    @Autowired
    public void setDoctorSchedulesMapper(DoctorSchedulesMapper doctorSchedulesMapper) {
        this.doctorSchedulesMapper = doctorSchedulesMapper;
    }

    @Autowired
    public void setDoctorMapper(DoctorMapper doctorMapper) {
        this.doctorMapper = doctorMapper;
    }


    @Override
    public Page<Doctor> list(Page<Doctor> doctorPage, Doctor doctor) {
        Page<Doctor> p=doctorMapper.findList(doctorPage,doctor);
        return p;
    }

    @Override
    @AutoFill(OperationType.INSERT)
    @CacheEvict(value = "doctor",key = "'getAllDoctor'")
    public boolean save(Doctor doctor) {
        return doctorMapper.insert(doctor) > 0;
    }

    @CacheEvict(value = "doctor",key = "'getAllDoctor'")
    @Override
    public boolean removeById(Integer id) {
        Integer i = doctorSchedulesMapper.selectNamalByDoctorId(id);
        //如果i不等于0说明这个医生还要有活动不能删除
        if (i != null) {
            throw new DsException("该医生还要举办义诊活动，不允许开除");
        }
        return doctorMapper.deleteById(id) > 0;
    }

    @CacheEvict(value = "doctor",key = "'getAllDoctor'")
    @Override
    @AutoFill(OperationType.UPDATE)
    public boolean updateById(Doctor doctor) {
        return doctorMapper.updateById(doctor) > 0;
    }

    @Cacheable(value = "doctor",key = "'getAllDoctor'")
    @Override
    public List<Doctor> getAllDoctor() {
        return doctorMapper.selectList(null);
    }
}
